﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using KSUNO_EMS.Models;

namespace KSUNO_EMS.Controllers
{
    public class ManagementJobApplicationController : EmsController
    {
        //
        // GET: /ManagmentJobApplication/

        public ActionResult Index()
        {
            return (ActionResult)VerifyAccess("Manager") ?? View();
        }
        public ActionResult Create(JobApplicantDTO jobApplication)
        {
            try
            {
                var dbApplicant =jobApplication.Id == 0? new JobApplicant(): Database.JobApplicants.SingleOrDefault(a => a.Id == jobApplication.Id);
                if(dbApplicant == null)
                    return new ProperJsonResult
                    {
                        Data = new AjaxResult(false, "Employee Not Found! Please try again.")
                    };
                Mapper.Map(jobApplication, dbApplicant);
                if(jobApplication.Id == 0)
               Database.JobApplicants.Add(dbApplicant);
                Database.SaveChanges();
                return new ProperJsonResult
                {
                    Data = new AjaxResult(true, dbApplicant.Id)
                };

            }
            catch (Exception ex)
            {

                return new ProperJsonResult
                {
                    Data = new AjaxResult(false, ex.ToString())
                };


            }
           
        }
        public ActionResult Delete(int id)
        {
            var data = Database.JobApplicants.SingleOrDefault(a => a.Id == id);
            if(data == null)
                return new ProperJsonResult
                {
                    Data = new AjaxResult(false, "Employee Not Found! Please try again.")
                };
            Database.JobApplicants.Remove(data);
            Database.SaveChanges();
            return new ProperJsonResult
            {
                Data = new AjaxResult(true)
            };
        }
        public ActionResult List()
        {
            return new ProperJsonResult() { HasDates = true, Data = new AjaxResult(true, Database.JobApplicants.ToList().Select(Mapper.Map<JobApplicant, JobApplicantDTO>).ToList()) };
        }
        public ActionResult Get(int id)
        {
            return new ProperJsonResult
                       {
                           Data =
                               new AjaxResult(true,
                                              Mapper.Map<JobApplicant, JobApplicantDTO>(
                                                  Database.JobApplicants.SingleOrDefault(a => a.Id == id)))
                       };
            
        }
    }
}
